//
//  CZStatusRetweetView.swift
//  GZWeibo009
//
//  Created by gzxzmac on 16/3/27.
//  Copyright © 2016年 gzxzmac. All rights reserved.
//

import UIKit

import SnapKit

/// 被转发微博的view
class CZStatusRetweetView: UIView {
    
    // MARK: - 属性
    var retweetViewBottomCons: Constraint?
    
    /// 被转发的微博模型
    var retweetStatus: CZStatus? {
        didSet {
            // 当别人给被转发的模型赋值后,将模型的内容赋给对应的view
            // @发微博人的名字: 微博内容
            guard let screen_name = retweetStatus?.user?.screen_name else {
                CZPrint(msg: "retweetStatus?.user?.screen_name 没有值")
                return
            }
            
            guard let text = retweetStatus?.text else {
                CZPrint(msg: "retweetStatus?.text没有值")
                return
            }
            
            retweetContentLabel.text = "@\(screen_name): \(text)"
            
            // 判断被转发的微博里面是否有配图
            self.retweetViewBottomCons?.uninstall()
            if let picutureURLs = retweetStatus?.storePictureURLs {
                // 有图片要显示,显示retweetPictureView
                retweetPictureView.hidden = false
                
                // 将被转发微博的图片URL赋值给retweetPictureView
                retweetPictureView.pictureURLs = picutureURLs
                
                // 重新设置CZStatusRetweetView的底部约束
                self.snp_makeConstraints(closure: { (make) -> Void in
                    self.retweetViewBottomCons = make.bottom.equalTo(retweetPictureView).offset(CZStatusCellMargin).constraint
                })
            } else {
                // 没有图片要显示
                retweetPictureView.hidden = true
                // 重新设置CZStatusRetweetView的底部约束
                self.snp_makeConstraints(closure: { (make) -> Void in
                    self.retweetViewBottomCons = make.bottom.equalTo(retweetContentLabel).offset(CZStatusCellMargin).constraint
                })
            }
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        preapreUI()
    }
    
    private func preapreUI() {
        // 添加子控件
        addSubview(bkgButton)
        addSubview(retweetContentLabel)
        addSubview(retweetPictureView)
        
        // 添加约束
        /// 背景按钮
        bkgButton.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(self)
        }
        
        /// 被转发文本
        retweetContentLabel.snp_makeConstraints { (make) -> Void in
            make.leading.top.equalTo(self).offset(CZStatusCellMargin)
            make.trailing.equalTo(self).offset(-CZStatusCellMargin)
        }
        
        /// 被转发的微博配图
        retweetPictureView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(retweetContentLabel)
            make.top.equalTo(retweetContentLabel.snp_bottom).offset(CZStatusCellMargin)
            make.size.equalTo(CGSize(width: 200, height: 200))
        }
        
        // CZStatusRetweetView底部和retweetContentLabel 重合,CZStatusRetweetView的高度根据子控件的内容来确定
        self.snp_makeConstraints { (make) -> Void in
            self.retweetViewBottomCons = make.bottom.equalTo(retweetPictureView).offset(CZStatusCellMargin).constraint
        }
    }
    
    // MARK: - 懒加载
    /// 背景按钮
    private lazy var bkgButton: UIButton = {
        let button = UIButton()
        
        // 设置背景
        button.setBackgroundImage(UIImage(named: "retweet_background"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "retweet_background_highlighted"), forState: UIControlState.Highlighted)
        
        return button
    }()
    
    /// 被转发微博内容label
    private lazy var retweetContentLabel: UILabel = {
        let label = UILabel()
        
        label.font = UIFont.systemFontOfSize(CZStatusCellContentSize)
        label.textColor = UIColor.darkGrayColor()
        label.numberOfLines = 0
        
        return label
    }()
    
    /// 微博配图
    private lazy var retweetPictureView: CZStatusPictureView = CZStatusPictureView()
}
